//获取地区经纬度数据
var MAPDATAURL = "https://data.jianshukeji.com/jsonp?filename=geochina/"

$(function(){
    getInfo();
})

function getInfo(){
    var url = MAPDATAURL + "china.json&callback=?"
    var provincesArr =[],provinces = [],citiesArr=[],cities = [],towns = [],citiesUrls = [];
    $.getJSON(url,function(data){
       //console.log(data);
       var provincesList = data.features;
       for(var i=0,j=provincesList.length;i<j;i++){
           var temp = provincesList[i].properties;
           //省市数据
           provinces.push({
               "div_id":temp.adcode,
               "div_name":temp.fullname,
               "drill_name":temp.drilldown,
               "file_name":temp.filename,
               "div_longitude":temp.longitude,
               "div_latitude":temp.latitude,
               "echart_name":temp.name,
               "parent_name":temp.parent,
               "children_num":temp.childrenNum,
                "div_level":temp.level
           });

           //div_id,div_name,drill_name,file_name,div_longitude,div_latitude,echart_name,parent_name,
           //children_num,div_level
           provincesArr.push([temp.adcode,temp.fullname,temp.drilldown,temp.filename,temp.longitude,
               temp.latitude,temp.name,temp.parent,temp.childrenNum,temp.level]);

           //各盟市数据
           url = MAPDATAURL + temp.drilldown+".json&callback=?"
           $.getJSON(url,function(data){
               //console.log(data);
               var citiesList = data.features;
               for(var k=0,m=citiesList.length;k<m;k++){
                   var temp2 = citiesList[k].properties;
                   cities.push({
                       "div_id":(temp2.adcode+"").replace(/00$/,''),
                       "div_name":temp2.fullname,
                       "drill_name":temp2.drilldown,
                       "file_name":temp2.filename,
                       "div_longitude":temp2.longitude,
                       "div_latitude":temp2.latitude,
                       "echart_name":temp2.name,
                       "parent_name":temp2.parent,
                       "parent_id":(temp2.adcode+"").substring(0,2),
                       "children_num":temp2.childrenNum,
                       "div_level":temp2.level=="district"?3:2
                   });

                   citiesArr.push([(temp2.adcode+"").replace(/00$/,''),temp2.fullname,temp2.drilldown,temp2.filename,temp2.longitude,
                       temp2.latitude,temp2.name,temp2.parent,(temp2.adcode+"").substring(0,2),temp2.childrenNum,(temp2.level=="district"?3:2)]);
                   //各区县数据
                   url = MAPDATAURL + temp2.drilldown+".json&callback=?"
                   citiesUrls.push(url)
                   // $.getJSON(url,function(data) {
                   //     console.log(data);
                   // });


               }
           });
       }

        console.log("=======provinces=======");
        console.log(provincesArr);
        console.log("=======cities=======");
        console.log(citiesArr);
        console.log("=======citiesUrls=======");
        console.log(citiesUrls);

        //使用JS保存csv文件到本地
        // var jsonObj = {
        //     'provinces':provincesArr,
        //     'cities':citiesArr,
        //     'citiesUrls':citiesUrls
        // }
        // var provincesFile = new File([JSON.stringify(jsonObj)], "provinces.txt", { type: "text/plain;charset=utf-8" });
        // saveAs(provincesFile);
    });
}




